<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class PeriodsController extends AppController {

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Periods','Services','PeriodsDetail');

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 * @throws NotFoundException When the view file could not be found
 *	or MissingViewException in debug mode.
 */


	public function index()
	{
		$periods=$this->Periods->find('all');
		//var_dump($periods);
		$this->set("data",$periods);
		//$this->render(false);

		$periodsdetail=$this->PeriodsDetail->find('all');
		//var_dump($periodsdetail);
		//$this->layout=false;
		//$this->render(false);	
	}


	public function add()
	{
		if(!empty($this->data)){
			var_dump($this->data);
			$this->Periods->set($this->data);
			if($this->Periods->validates()){
				if($this->Periods->save($this->data)){
					$this->redirect('/periods');
				}
		}
		else{
			$errors = $this->Periods->validationErrors;
				var_dump($errors);
				//$this->layout=false;
				//$this->render(false);	
				//$this->Session->setFlash(__("Error", true), 'flash', array('class' => 'alert alert-error'));
			}	

			$this->layout=false;
			$this->render(false);	
		}
		$services=$this->Services->find('all');
		
		$this->set("services",$services);
	}

	public function edit($id)
	{
		if (!empty($this->data))
		{					
    	$this->Periods->id = $id;        
    	if ($this->Periods->save($this->data)) 
    	{
    	  //$this->Session->setFlash('Socio Actualizado.', 'flash', array('class' => 'thrill'));
        $this->redirect('/periods');
      }
    }
    else {        
    	$row= $this->Periods->findById($id);
			//var_dump($row);
			$new_date = ($row['Periods']['experition1']!='')? date("d-m-Y", strtotime($row['Periods']['experition1'])) : '';
			$row['Periods']['experition1'] = $new_date;
			$new_date = ($row['Periods']['experition2']!='')? date("d-m-Y", strtotime($row['Periods']['experition2'])) : '';
			$row['Periods']['experition2'] = $new_date;
			$new_date = ($row['Periods']['experition3']!='')? date("d-m-Y", strtotime($row['Periods']['experition3'])) : '';
			$row['Periods']['experition3'] = $new_date;
			
			$this->request->data =$row;
    }

		$this->render("add");
	}

	public function get_years() {
		$this->layout=false;
		$this->render(false);

		$rows = $this->Periods->find('all',array(
			'fields'=>"Periods.year",
			'group' => array('Periods.year'),
			'order' => array('Periods.year DESC'),
			));
		//var_dump($rows);
		$years=[];
		foreach ($rows as $item) {
			//var_dump($item);
			$years[]=$item['Periods']['year'];
		}
		//var_dump($years);
		echo json_encode($years, TRUE);
	}
}
